﻿using Clotho.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Clotho.Novelview {
	public class NovelViewFeature : IFeature, IUiFeature {
		public IEnumerable<ControlKeeper> Create_control_table => create_controls;
		ControlKeeper[] create_controls;

		Point location;
		public event EventHandler eLocationChanged;
		public Point Location {
			get {
				return location;
			}
			set {
				var old = location;
				Nov.doCmd(() => {
					location = value;
					eLocationChanged?.Invoke(this,null);
				}, () => {
					location = old;
					eLocationChanged?.Invoke(this, null);
				});
			}
		}
		public void move(int x, int y) {
			Nov.doCmd(() => {
				location.Offset(x, y);
				eLocationChanged?.Invoke(this, null);
			}, () => {
				location.Offset(-x, -y);
				eLocationChanged?.Invoke(this, null);
			});
		}



		public NovelViewFeature(IItem parent, string name) : base(parent, name) {
			create_controls = new ControlKeeper[] {
				new ControlKeeper(create_str => new NovelviewControl(Nov, this) ,Name,()=>$"{NovelViewFeature.TYPEWORD}-{Name}","NovelView"),
			};
			this.eNameChanging += NovelViewFeature_eNameChanged;
		}

		private void NovelViewFeature_eNameChanged(object sender, TEventArgs<string> e) {
			foreach(Plot p in Nov) {
				var vf = p.getFeature<ViewFeature>(Name);
				vf?.focusChangeName(e.Value);
			}
			foreach (var ck in create_controls) {
				ck.Head_text = e.Value;
			}
		}

		public const string TYPEWORD = "情节图";
		public override string Type_word => TYPEWORD;


		public override IFeature Clone(IItem parent) {
			var rev =  new NovelViewFeature(parent, Name);
			return rev;
		}
		protected override void writeDatas(StringBuilder sb) {
			writeString(sb,location.X.ToString());
			writeString(sb, location.Y.ToString());
			return;
		}
		public override void loadData(string[] ss) {
			location = new Point(Int32.Parse(ss[1]), Int32.Parse(ss[2]));
		}


	}
}
